import pandas as pd
import numpy as np
import h5py
import os
from sklearn.neighbors import KNeighborsClassifier as KNN
print('开始读取data.txt文件...')
df = pd.read_csv('data/data.txt',names=['id', 'longitude', 'latitude', 'bs_label'],header=0)
df1=df.sort_values(by='bs_label')
num=df1['bs_label'].value_counts(sort=False).values  #对重复数据列进行次数统计 num:每个基站对应栅格数

print('开始读取北京流量数据...')
unicom1=pd.DataFrame({})
unicom = pd.read_excel('data/beijing_haidian.xlsx', sheet_name=0,usecols=[0, 2, 3, 6, 7],names=["datetime","longitude","latitude","internet_in","internet_out"])
unicom1=unicom1.append(unicom)
unicom1=unicom1.set_index('datetime')
unicom1['internet']=unicom1['internet_in']+unicom1['internet_out']

unicom1=unicom1.groupby(['datetime','longitude','latitude']).sum()
unicom2 = unicom1.reset_index()
unicom2=unicom2.set_index('datetime')

unicom = unicom.drop_duplicates(subset=['longitude', 'latitude'])
##knn打标签
print('开始KNN预测...')
list1=unicom['longitude'].values
list2=unicom['latitude'].values
train_list=list(zip(list1,list2))
train_list=np.array(train_list)
a=list(range(1,172))
a=np.array(a)
X = train_list   #训练集
y = a                    #标签
neigh = KNN(n_neighbors=1)
neigh.fit(X, y)

print('开始逐小时处理数据...')
for day in range(1,6):
    for index in range(24):
        print('2021-09-' +str(day)+' '+ str(index) + '时...')
        unicom3 = unicom2.loc['2021-09-'+str(day).zfill(2)+' '+ str(index).zfill(2)]
        l1 = unicom3['longitude'].values
        l2 = unicom3['latitude'].values
        l3 = list(zip(l1, l2))
        l3 = np.array(l3)  # 每小时经纬度
        internet = unicom3['internet'].values  # 每个基站对应流量
        base_label = neigh.predict(l3)
        # 计算每个基站对应小栅格的流量
        size = internet.shape[0]
        tr = np.zeros(171)
        for i in range(size):
            tr[base_label[i] - 1] = internet[i] / num[base_label[i] - 1]
        # 计算每个大栅格流量
        Cell = np.zeros(1560)
        for m in range(40):
            print('第' + str(m) + '行...')
            for n in range(39):
                sum = 0
                ID = 39 * m + n
                if ID % 40 == 0:
                    id = m * 5 * 5 * 39
                else:
                    id = m * 5 * 5 * 39 + n * 5
                for j in range(5):
                    for k in range(5):
                        temp = df[df.id == j * 5 * 39 + id + k]['bs_label'].values[0]
                        sum = sum + tr[temp - 1]
                Cell[ID] = sum

        Cell = Cell * 1000
        Cell = Cell.reshape(1, 1560)
        if index == 0:
            day_Cell = Cell
        else:
            day_Cell = np.append(day_Cell, Cell, axis=0)

    # 保存为H5文件
    print('creating H5file...')

    # idx=list(range(1763))
    # idx=np.array(idx)
    if not os.path.exists('09-'+str(day).zfill(2)+'.h5'):
        with h5py.File('09-'+str(day).zfill(2)+'.h5', 'w') as f:
            f['data'] = day_Cell
            # f['idx'] = idx







